package com.yc.baihe.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AgeUtil {
	public static int getAge(String birthDate) {

		if (birthDate == null){
			throw new RuntimeException("出生日期不能为null");
		}
		int age = 0;

		Date now = new Date();

		SimpleDateFormat format_y = new SimpleDateFormat("yyyy");
		SimpleDateFormat format_M = new SimpleDateFormat("MM");
		SimpleDateFormat format_d = new SimpleDateFormat("dd");

		String birth_year = birthDate.split("-")[0];
		String this_year = format_y.format(now);

		String birth_month = birthDate.split("-")[1];
		String this_month = format_M.format(now);
		
		String birth_day = birthDate.split("-")[2];
		String this_day = format_d.format(now);

		// 初步，估算
		age =Integer.parseInt(this_year) - Integer.parseInt(birth_year);

		// 如果未到出生月份，则age - 1
		if (this_month.compareTo(birth_month)<0 || ((this_month.compareTo(birth_month)==0)&&(this_day.compareTo(birth_day)<0)) ){
			age -= 1;
		}
		if (age < 0){
			age = 0;
		}
		return age;
	} 
}
